import os
import webbrowser
import wx.adv
from config import global_config
from gui.gui_logic import DownloadThread


class PanelData(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.init_ui()


    def init_ui(self):
        panel = self
        main_sizer = wx.BoxSizer(wx.VERTICAL)

        # 第一行：路径显示
        path_sizer = wx.BoxSizer(wx.HORIZONTAL)
        path_label = wx.StaticText(panel, label="当前保存路径:")
        self.path_display = wx.StaticText(panel, label=global_config.data_dir)
        self.open_link = wx.adv.HyperlinkCtrl(panel, -1, "打开目录", url="")
        self.open_link.Bind(wx.adv.EVT_HYPERLINK, self.on_open_dir)
        tip_label = wx.StaticText(panel, label="（可在系统配置中修改）")
        tip_label.SetForegroundColour(wx.Colour(128, 128, 128))

        path_sizer.Add(path_label, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
        path_sizer.Add(self.path_display, 1, wx.EXPAND | wx.RIGHT, 5)
        path_sizer.Add(self.open_link, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
        path_sizer.Add(tip_label, 0, wx.ALIGN_CENTER_VERTICAL)
        main_sizer.Add(path_sizer, 0, wx.EXPAND | wx.ALL, 10)

        # 第二行：更新按钮
        btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.btn_update = wx.Button(panel, label="历史数据下载（akshare）")
        #self.btn_pause = wx.Button(panel, label="暂停")
        self.btn_stop = wx.Button(panel, label="停止")

        # 绑定事件
        self.btn_update.Bind(wx.EVT_BUTTON, self.on_update)
        #self.btn_pause.Bind(wx.EVT_BUTTON, self.on_pause)
        self.btn_stop.Bind(wx.EVT_BUTTON, self.on_stop)

        # 将按钮添加到水平sizer中
        btn_sizer.Add(self.btn_update, 1, wx.EXPAND)
        #btn_sizer.Add(self.btn_pause, 0, wx.LEFT, 5)
        btn_sizer.Add(self.btn_stop, 0, wx.LEFT, 5)

        # 将水平sizer添加到主sizer
        main_sizer.Add(btn_sizer, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)

        # # 第三行
        # btn_sizer_bond = wx.BoxSizer(wx.HORIZONTAL)
        # self.btn_update_bond = wx.Button(panel, label="更新全量可转债数据（1042）")
        # # self.btn_pause = wx.Button(panel, label="暂停")
        # self.btn_stop_bond = wx.Button(panel, label="停止")
        #
        # # 绑定事件
        # self.btn_update_bond.Bind(wx.EVT_BUTTON, self.on_update_bond)
        # # self.btn_pause.Bind(wx.EVT_BUTTON, self.on_pause)
        # self.btn_stop_bond.Bind(wx.EVT_BUTTON, self.on_stop_bond)

        # # 将按钮添加到水平sizer中
        # btn_sizer_bond.Add(self.btn_update_bond, 1, wx.EXPAND)
        # # btn_sizer.Add(self.btn_pause, 0, wx.LEFT, 5)
        # btn_sizer_bond.Add(self.btn_stop_bond, 0, wx.LEFT, 5)
        #
        # # 将水平sizer添加到主sizer
        # main_sizer.Add(btn_sizer_bond, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)

        # self.btn_update = wx.Button(panel, label="更新全量ETF数据（1499支）")
        # self.btn_update.Bind(wx.EVT_BUTTON, self.on_update)
        # main_sizer.Add(self.btn_update, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)

        # 第三行：进度条
        self.progress = wx.Gauge(panel, range=100, size=(-1, 25))
        main_sizer.Add(self.progress, 0, wx.EXPAND | wx.ALL, 10)

        # 第四行：日志文本框
        self.log_text = wx.TextCtrl(panel, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL)
        main_sizer.Add(self.log_text, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 10)

        panel.SetSizer(main_sizer)

    def on_open_dir(self, event):
        webbrowser.open(global_config.data_dir)

    def on_pause(self, event):
        # 暂停逻辑
        pass

    def on_stop(self, event):
        # 停止逻辑
        self.download_thread.stop()

    def on_update(self, event):
        self.btn_update.Disable()
        self.log_text.Clear()
        self.download_thread = DownloadThread(self, 'etf')
        self.download_thread.start()

    def on_stop_bond(self, event):
        # 停止逻辑
        self.download_thread_bond.stop()

    def on_update_bond(self, event):
        self.btn_update_bond.Disable()
        self.log_text.Clear()
        self.download_thread_bond = DownloadThread(self, 'bond')
        self.download_thread_bond.start()

    def update_progress(self, value, log_msg):
        self.progress.SetValue(value)
        self.log_text.AppendText(log_msg)

    def enable_update_button(self):
        self.btn_update.Enable()

    def enable_update_bond_button(self):
        self.btn_update_bond.Enable()